package com.masterclient.app.modules.category.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.masterclient.app.R;
import com.masterclient.app.modules.category.beans.CategoryAllIndexBean;
import com.eoner.baselibrary.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/6/30 13:43
 * 功能说明：
 */

public class CategoryBrandGridAdapter extends BaseAdapter {


    List<CategoryAllIndexBean.ShCategorysBean.ShBrandsBean> list;
    private Context context;

    public CategoryBrandGridAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_category_2, null);
            holder.ivBrandLogo = (ImageView) convertView.findViewById(R.id.iv_brand_logo);
            holder.tvBrandName = (TextView) convertView.findViewById(R.id.tv_brand_name);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        CategoryAllIndexBean.ShCategorysBean.ShBrandsBean brandsBean = list.get(position);
        if(!TextUtils.isEmpty(brandsBean.getSh_logo())){
            GlideUtil.displayRoundImage(context,brandsBean.getSh_logo(),holder.ivBrandLogo,7,R.mipmap.defaults);
        }
        holder.tvBrandName.setText(brandsBean.getSh_name());
        return convertView;
    }

    public void setList(List<CategoryAllIndexBean.ShCategorysBean.ShBrandsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setCategoryList(List<CategoryAllIndexBean.ShCategorysBean.ShChildsBean> listBean){
        List<CategoryAllIndexBean.ShCategorysBean.ShBrandsBean> brandList = new ArrayList<>();
        for (CategoryAllIndexBean.ShCategorysBean.ShChildsBean childBran : listBean) {
            CategoryAllIndexBean.ShCategorysBean.ShBrandsBean brand = new CategoryAllIndexBean.ShCategorysBean.ShBrandsBean();
            brand.setSh_category_logo(childBran.getSh_small_image());
            brand.setSh_id(childBran.getSh_id());
            brand.setSh_name(childBran.getSh_name());
            brandList.add(brand);
        }
        this.list = brandList;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        ImageView ivBrandLogo;
        TextView tvBrandName;
    }
}
